import time
import ctypes
import os

# 获取当前运行的脚本的完整路径
script_path = os.path.abspath(__file__)
# print(f"当前运行的脚本路径: {script_path}")

# 获取当前运行的脚本所在的目录路径
script_dir = os.path.dirname(script_path)
# print(f"当前运行的脚本所在的目录路径: {script_dir}")
# 载入c++动态库
libLoad = ctypes.cdll.LoadLibrary
libPath = f"{script_dir}/build/libsharedTest.so"
print(libPath)
sharelib = libLoad(libPath)

class SharedData(ctypes.Structure):
    _fields_ = [
        ('type', ctypes.c_int),
        ('data', ctypes.c_double * 6)
    ]

shareddata = SharedData()
shareddata.type = 5
shareddata.data[0] = 1
shareddata.data[1] = 2
shareddata.data[2] = 3
shareddata.data[3] = 4
shareddata.data[4] = 5
shareddata.data[5] = 6
sharelib.setdata.argtype = SharedData
# sharelib.setdata(shareddata)
sharelib.getdata.restype = SharedData
shareddata = sharelib.getdata()
print(shareddata.type, shareddata.data[0], shareddata.data[1], shareddata.data[2], shareddata.data[3], shareddata.data[4], shareddata.data[5])
while True:
    shareddata = sharelib.getdata()
    # shareddata.data[0] += 1
    sharelib.setdata(shareddata)
    time.sleep(0.008)# 8ms loop